﻿using Microsoft.Data.Sqlite;
using SendMailServer.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SendMailServer.Wish
{
    internal class Wishboard
    {
        public static ResMsg HandlerMsg(string key, string filterStr)
        {
            if (key == "GetWishboard")
            {
                if (filterStr == null) { filterStr = ""; }
                if (filterStr.EndsWith(",desktop"))
                {
                    filterStr = filterStr.Substring(0, filterStr.Length - ",desktop".Length);
                }

                var res = GetSnips(filterStr);
                StringBuilder stringBuilder = new StringBuilder();
                for (var i = 0; i < res.Count; i++)
                {
                    stringBuilder.Append(res[i].ToFormatString());
                    stringBuilder.Append("<%>");
                }
                return new ResMsg()
                {
                    MsgCode = key,
                    AppName = key,
                    Content = stringBuilder.ToString(),
                    IsOk = true,
                    UseSelfResponse = true,
                };
            }

            return null;
        }

        public static List<WishboardItem> GetSnips(string filter)
        {
            StringBuilder debuggerStr = new StringBuilder();
            Microsoft.Data.Sqlite.SqliteConnection conn = null;
            Microsoft.Data.Sqlite.SqliteCommand cmd = null;
            SqliteDataReader dr = null;
            List<WishboardItem> res = new List<WishboardItem>();
            try
            {
                using (conn = DatabaseHelper.BuildConnection())
                {
                    string sql = "select  * from wishBoard";
                    cmd = new Microsoft.Data.Sqlite.SqliteCommand(sql, conn);
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        WishboardItem itm = new WishboardItem()
                        {
                            qID = (long)(dr["id"]),
                            creator = dr["creator"] as string,
                            wishContent = dr["wishContent"] as string,
                            isrealing = dr["isrealing"] as string,
                            realer = dr["realer"] as string,
                            createtime = dr["createtime"] as string
                        };

                        res.Add(itm);
                    }
                }
            }
            catch (Exception err)
            {
                res.Add(new WishboardItem()
                {
                    qID = -1,
                    hasError = true,
                    creator = "错误",
                    wishContent = "发生了错误:" + err.Message + "," + err.StackTrace
                });
                return res;
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
            return res;
        }
    }
    class WishboardItem
    {
        public bool hasError { get; set; } = false;
        public long qID;
        public string creator;
        public string wishContent;
        public string isrealing;
        public string realer;
        public string createtime;

        public string ToFormatString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(qID.ToString());
            stringBuilder.Append("@"+ creator.ToString());
            stringBuilder.Append("@" + Regex.Replace(wishContent.ToString(),"@","at"));
            stringBuilder.Append("@" + isrealing);
            stringBuilder.Append("@" + realer);
            stringBuilder.Append("@" + createtime);
            return stringBuilder.ToString();
        }
    }
}
